<!--任务切换分组-->
<template>
  <dialog-edit :visible.sync="dialogVisible" :title="$attrs.title" ref="userManageEdit" width="500" @save="save" :remember="false" :destroyOnClose="true"
               :height="160" :saveFlag="true" :loading="loading"
               :escClosable="true" :closeFlag="true" :saveLoading="saveLoading">
    <template v-slot:body>
      <div style="margin-bottom: 10px">
        <vxe-form :data="preFormData" :span="24" title-align="right" :rules="formRules"
                  :title-width="60" ref="formRefs">
          <div class="allq">
            <vxe-form-item title="任务" field="Parenttaskid">
              <template v-slot>
                <select-task ref="selectTask" :queryValue="queryValue" @change="change"></select-task>
              </template>
            </vxe-form-item>
          </div>
        </vxe-form>
      </div>
    </template>
  </dialog-edit>
</template>

<script>
import dialogEdit from '@/components/element/dialogEdit'
import * as format from "@/filters";
import SelectTask from "@/components/element/select/SelectTask";
import projectTaskApi from "@/api/projectTaskApi";
export default {
  name: "dialogTask",
  components: {
    dialogEdit,
    SelectTask
  },
  props: {
    queryValue: {
      type: Object,
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    formData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data () {
    return {
      preFormData: {},
      saveLoading: false,
      loading: false,
      Parenttaskid: undefined,
      formRules: {
        Parenttaskid: [{
          required: true, message: '分组必须选择', trigger: 'change'
        }]
      }
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (value) {
        this.$emit('update:visible', value)
      }
    },
  },
  methods: {
    change (w) {
      if (w.length > 0) {
        this.Parenttaskid = w[w.length - 1]
        console.log(this.Parenttaskid, this.preFormData.Id);
      }
    },
    save () {
      console.log(this.preFormData)
      if (this.preFormData.Id === this.Parenttaskid) {
        return this.$message.warning('分组不能切换到本身')
      }
      this.preFormData.Parenttaskid = this.Parenttaskid
      if (this.preFormData.Parenttaskid) {
        this.preFormData.id = this.preFormData.Id
        projectTaskApi.update(this.preFormData).then(res => {
          this.$message({ type: res.code === 1 ? 'success' : 'warning', message: res.message })
          this.$nextTick(() => {
            this.dialogVisible = false
            if (res.code === 1) {
              this.$emit('switchParenttas')
            }
          })
        })
      } else {
        this.$message.warning('请先选择切换分组')
      }
    },
  },
  watch: {
    dialogVisible: {
      handler (n, o) {
        console.log(n, o)
        this.preFormData = JSON.parse(JSON.stringify(this.formData))
        console.log(this.preFormData);
        this.$nextTick(() => {
          if (this.$refs.selectTask) {
            this.$refs.selectTask.load = !this.$refs.selectTask.load
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.wange >>> .w-e-text{
  overflow-y: hidden;
}
.allq >>> .vxe-form--item {
  width: 100%;
}
</style>
